#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QDebug>
#include<QColorDialog>
#include<QFileDialog>
#include<QFontDialog>
#include<QInputDialog>
#include<QMessageBox>
#include<QProgressDialog>
#include<QThread>
#include<QtConcurrent/QtConcurrent>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    QColor c = QColorDialog::getColor();
    qDebug()<< c;
}

void MainWindow::on_pushButton_5_clicked()
{
    QStringList fs = QFileDialog::getOpenFileNames();
    qDebug()<<fs;
}

void MainWindow::on_pushButton_2_clicked()
{
    QFont f = QFontDialog::getFont(nullptr);
    qDebug() << f;
}

void MainWindow::on_pushButton_4_clicked()
{
    QString t = QInputDialog::getText(this,"Title","label");
    qDebug() <<t;
}

void MainWindow::on_pushButton_3_clicked()
{
    int i = QMessageBox::information(this,"title","text",QMessageBox::Ok);
    qDebug() << i;
}

void MainWindow::on_pushButton_6_clicked()
{

    QProgressDialog progress("Copying files...", "Abort Copy", 0, 100, this);

    progress.setWindowModality(Qt::WindowModal);

//    progress.setValue(1);



    connect(this,SIGNAL(updateProgressUIValue(int)),&progress,SLOT(setValue(int)));

//    QTimer::singleShot(1000,[](){

//        qDebug()<<"timer";

//    });

    QtConcurrent::run([this,&progress](){
        qDebug()<< "thread";
        for(int i = 0; i <= 100; i++){
            qDebug() << "wasCanceled "<<progress.wasCanceled();
            if(progress.wasCanceled()){
                break;
            }
            QThread::sleep(1);
            emit updateProgressUIValue(i);

        }
        qDebug() << "emit over";

    });


     qDebug()<< progress.exec();
    qDebug() << "wasCanceled "<<progress.wasCanceled();

//    progress.show();

//    for (int i = 0; i < 100; i++) {

//        progress.setValue(i);

//        if (progress.wasCanceled())
//            break;
//        //... copy one file
//        QThread::sleep(1);
//        QCoreApplication::processEvents();
//    }

//    progress.setValue(100);

        //auto disconnct!!!
//    disconnect(this,SIGNAL(updateProgressUIValue(int)),&progress,SLOT(setValue(int)));
}
